<?php

namespace Common\Model;

/**
 * 货币表
 * @modify wenhui 2018-01-30 创建
 */
class CurrencyExchangeRateModel extends BaseModel {
    protected $trueTableName = 'tb_currency_exchange_rate';
    protected $field = 'id,before_currency_id,after_currency_id,rare_value,updatetime';

     /**
     * 获取货币列表
     * @modify wenhui 2018-01-30 创建
     * @param  integer   $currencyId    转化前货币 tb_currency.id
     * @param  integer   $transCurrencyId    转化后货币 tb_currency.id
     * @return array
     */
    public function checkTransRateExist($currencyId, $transCurrencyId) {
        $option['before_currency_id'] = $currencyId;
        $option['after_currency_id'] = $transCurrencyId;
        $option['isdelete'] = NOT_DELETE;

        $data = $this->field($this->field)->where($option)->find();

        return empty($data) ? array() : $data;
    }

    /**
     * 添加条目
     * @modify wenhui 2018-01-30 创建
     * @param  integer   $currencyId    转化前货币 tb_currency.id
     * @param  integer   $transCurrencyId    转化后货币 tb_currency.id
     * @param  decimal   $rateValue  转化汇率值
     */
    public function addCurrencyRate($currencyId, $transCurrencyId, $rateValue) {
        $data = array(
            'before_currency_id' => $currencyId,
            'after_currency_id' => $transCurrencyId,
            'rare_value' => $rateValue,
        );

        $result = $this->add($data);
        return empty($result) ? false : $result;
    }

    /**
     * 修改信息
     * @modify wenhui 2018-01-30 创建
     * @param  integer   $currencyId    转化前货币 tb_currency.id
     * @param  integer   $transCurrencyId    转化后货币 tb_currency.id
     * @param  decimal   $rateValue  转化汇率值
     */
    public function saveCurrencyRate($currencyId, $transCurrencyId, $rateValue) {
        $option = array(
            'before_currency_id' => $currencyId,
            'after_currency_id' => $transCurrencyId,
        );

        $data['rare_value'] = $rateValue;
        
        return $this->where($option)->save($data) === false ? false : true;
    }
}
